package com.xique.door.controller;


import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.request.AccessDevice.AccessDeviceAddRequest;
import com.xique.door.bean.request.AccessDevice.AccessDeviceEditRequest;
import com.xique.door.bean.request.AccessDevice.AccessDeviceRemoveRequest;
import com.xique.door.biz.service.IAccessDeviceBizService;
import com.xique.door.service.IAccessDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 门禁设备Controller
 *
 * @author ww
 * @date 2021-06-10
 */
@Controller
@RequestMapping("/oms/accessDevice")
public class AccessDeviceController extends BaseController {

    @Autowired
    private IAccessDeviceService accessDeviceService;

    @Autowired
    private IAccessDeviceBizService accessDeviceBizService;

    /**
     * 查询门禁设备列表
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:list")
    @PostMapping("/list/{parkId}")
    @ResponseBody
    public TableDataInfo list(AccessDevice accessDevice) {
        startPage();
        List<AccessDevice> list = accessDeviceService.selectAccessDeviceList(accessDevice);
        return getDataTable(list);
    }

    /**
     * 导出门禁设备列表
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:export")
    @PostMapping("/export/{parkId}")
    @ResponseBody
    public void export(HttpServletResponse response, AccessDevice accessDevice) throws IOException {
        List<AccessDevice> list = accessDeviceService.selectAccessDeviceList(accessDevice);
        ExcelUtil<AccessDevice> util = new ExcelUtil<AccessDevice>(AccessDevice.class);
        util.exportExcel(response, list, "accessDevice");
    }

    /**
     * 新增保存门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:add")
    @Log(title = "门禁设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccessDevice accessDevice) {
        return toAjax(accessDeviceService.insertAccessDevice(accessDevice));
    }

    /**
     * 新增保存门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:add")
    @Log(title = "门禁设备", businessType = BusinessType.INSERT)
    @PostMapping("/add/v2")
    @ResponseBody
    public AjaxResult addSaveV2(AccessDeviceAddRequest request) {
        return accessDeviceBizService.addDevice(request);
    }

    /**
     * 修改保存门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:edit")
    @Log(title = "门禁设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccessDevice accessDevice) {
        return toAjax(accessDeviceService.updateAccessDevice(accessDevice));
    }

    /**
     * 修改保存门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:edit")
    @Log(title = "门禁设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/v2")
    @ResponseBody
    public AjaxResult editSaveV2(AccessDeviceEditRequest request) {
        return accessDeviceBizService.editDevice(request);
    }

    /**
     * 删除门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:remove")
    @Log(title = "门禁设备", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(accessDeviceService.deleteAccessDeviceByIds(Arrays.asList(Convert.toLongArray(ids))));
    }

    /**
     * 删除门禁设备
     */
    @PreAuthorize(hasPermi = "oms:accessDevice:remove")
    @Log(title = "门禁设备", businessType = BusinessType.DELETE)
    @PostMapping("/remove/v2")
    @ResponseBody
    public AjaxResult removeV2(AccessDeviceRemoveRequest request) {
        return accessDeviceBizService.removeDevice(request);
    }

    /**
     * 设备状态修改
     */
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "oms:accessDevice:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Long id, String status) {
        AccessDevice update = new AccessDevice();
        update.setId(id);
        update.setStatus(status);
        int count = accessDeviceService.updateAccessDevice(update);
        return toAjax(count);
    }

    /**
     * 可以下发的设备
     */
    @GetMapping("/issueAccessDevice")
    @ResponseBody
    public AjaxResult selectIssueAccessDevice(String districtName) {

        return AjaxResult.success(accessDeviceService.selectIssueAccessDevice(districtName));
    }


}
